package mcom.xmks.bluetooth.mesh.utils;


import java.util.Locale;
public class ConvertUtil {
    /**
     * 16进制字符串转字节数据
     * @param hex
     * @return
     */
    public static byte[] fromHexString2ByteArray(String hex){
        //"([0-9|a-f|A-F]{2}){16}"
        hex = hex.toUpperCase();
        int len = hex.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            b[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
        }
        return b;
//        if(hex.matches("([0-9|a-f|A-F]{2})+")){
//            int length = hex.length() / 2;
//            byte[] bArr = new byte[length];
//            for (int i = 0; i < length; i++) {
//                String substring = hex.substring(i*2, i*2 + 2);
//                int toInt = Integer.parseInt(substring,16);
//                bArr[i]=(byte) toInt;
//            }
//            return bArr;
//        }
//        return null;
    }

    /**
     * 字节数据转16进制字符串
     * @param bytes
     * @return
     */
    public static String fromByteArray2HexString(byte[] bytes){
        StringBuffer sb = new StringBuffer(bytes.length);
        String sTemp;
        for (int i = 0; i < bytes.length; i++) {
            sTemp = Integer.toHexString(0xFF & bytes[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 字节数组翻转
     * @param bytes
     * @return
     */
    public static  byte[] flipByteArray(byte[] bytes){
        byte[] target=new byte[bytes.length];
        for(int i=0;i<bytes.length;i++ ){
            target[i]=bytes[bytes.length-1-i];
        }
        return target;
    }
    /**
     * 字符串转换成十六进制字符串(采用utf-8编码)
     *
     * @param str 待转换的ASCII字符串
     * @return String 每个Byte之间空格分隔，如: [616C6B]
     */
    public static String str2HexStr(String str) {

        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;

        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            //sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
     * 十六进制转换字符串(采用utf-8编码)
     *
     * @param hexStr Byte字符串(Byte之间无分隔符 如:[616C6B])
     * @return String 对应的字符串
     */
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;

        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }
    /**
     * 获取节点16进制字符串
     * @param nodeAddress
     * @return
     */
    public static String getNodeHexString(int nodeAddress){
        return String.format(Locale.getDefault(),"%04X",nodeAddress);
    }
    /**
     * 截取byte数组   不改变原数组
     * @param b 原数组
     * @param offset 偏差值（索引）
     * @param length 长度
     * @return 截取后的数组
     */
    public static byte[] subByte(byte[] b,int offset,int length){
        byte[] b1 = new byte[length];
        System.arraycopy(b, offset, b1, 0, length);
        return b1;
    }
}
